package jxt.elt.jobs;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Locale;

import jxt.elt.common.DbPool;
import jxt.elt.common.SendEmailBean;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

//邮件发送
//发送失败后没有再次处理
public class EmailSending implements Job {

	public void execute(JobExecutionContext context)
			throws JobExecutionException {

		Connection conn = DbPool.getInstance().getConnection();

		try {
			conn.setAutoCommit(false);
			PreparedStatement preparedStatement = conn
					.prepareStatement("select nid,qy,yg,fsyx,jsyx,bt,nr,fslx,fsdj from tbl_yjdf where ljfs=1 order by fsdj desc limit 1");
			ResultSet rs = null;

			String nid = "";
			String jsyx = "";
			String bt = "";
			String nr = "", yg = "", fsyx = "", fslx = "", fsdj = "";
			int qy = 0;
			Boolean szt = Boolean.valueOf(false);
			rs = preparedStatement.executeQuery();
			if (rs.next()) {
				nid = rs.getString("nid");
				jsyx = rs.getString("jsyx");
				bt = rs.getString("bt");
				nr = rs.getString("nr");
				qy = rs.getInt("qy");

				yg = rs.getString("yg");
				fsyx = rs.getString("fsyx");
				fslx = rs.getString("fslx");
				fsdj = rs.getString("fsdj");
			}
			rs.close();
			if (jsyx != null && jsyx.length() > 0) {
				preparedStatement = conn
						.prepareStatement("delete from tbl_yjdf where nid=?");
				preparedStatement.setString(1, nid);
				preparedStatement.executeUpdate();
			}
			conn.commit();

			conn.setAutoCommit(false);
			if (jsyx != null && jsyx.length() > 0) {
				SendEmailBean seb = new SendEmailBean(qy);
				szt = seb.sendHtmlEmail(jsyx, nr, bt);
				String strsql = "insert into tbl_yjyf (qy,yg,fsyx,jsyx,bt,nr,fssj,fslx,fsdj,fszt) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
				// if (szt)
				// strsql="insert into tbl_yjyf (qy,yg,fsyx,jsyx,bt,nr,fssj,fslx,fsdj,fszt) select qy,yg,fsyx,jsyx,bt,nr,now(),fslx,fsdj,1 from tbl_yjdf where nid="+nid;
				// else
				// strsql="insert into tbl_yjyf (qy,yg,fsyx,jsyx,bt,nr,fssj,fslx,fsdj,fszt) select qy,yg,fsyx,jsyx,bt,nr,now(),fslx,fsdj,0 from tbl_yjdf where nid="+nid;
				preparedStatement = conn.prepareStatement(strsql);
				preparedStatement.setInt(1, qy);
				preparedStatement.setString(2, yg);
				preparedStatement.setString(3, fsyx);
				preparedStatement.setString(4, jsyx);
				preparedStatement.setString(5, bt);
				preparedStatement.setString(6, nr);
				preparedStatement.setTimestamp(7, new Timestamp(Calendar
						.getInstance(Locale.CHINA).getTimeInMillis()));
				preparedStatement.setString(8, fslx);
				preparedStatement.setString(9, fsdj);
				if (szt) {// 发送成功
					preparedStatement.setInt(10, 1);
				} else { // 发送失败
					preparedStatement.setInt(10, 0);
				}
				preparedStatement.executeUpdate();
			}

			conn.commit();
			preparedStatement.close();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (Exception ee) {
			}
		} finally {
			try {
				if (!conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException ex) {
				ex.printStackTrace();
			}
		}
	}

}
